package com.newyulong.implatform.activity;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.newyulong.implatform.IMPApplication;
import com.newyulong.implatform.R;
import com.newyulong.implatform.bean.UserInfoJsonBean;
import com.newyulong.implatform.db.UserDao;
import com.newyulong.implatform.defines.Defines;
import com.newyulong.implatform.util.CommonsUtil;

public class LoginActivity extends Activity {
	private Button btnLogin;
	private ImageView ivCheckbox;
	boolean isChecked = true;
	private EditText etUserName;
	private EditText etPassword;
	private Dialog pd;
	
	String userName = "";
	String password = "";
	String openId;
	
	private static final int LOGIN_SECUSS = 1;
	private static final int LOGIN_FAILED = 2;
	
	Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			int what = msg.what;
			switch (what) {
			case Defines.HTTP_REQUEST_TIMEOUT:
				//网络异常
				CommonsUtil.createInfoDialog(LoginActivity.this, "网络异常！");
				break;
			case LOGIN_SECUSS:
				//登录成功
				handlUserInfo();
				break;
			case LOGIN_FAILED:
				//登录失败
				CommonsUtil.createInfoDialog(LoginActivity.this, "用户名或密码错误！");
				break;
			default:
				break;
			}
			
			pd.dismiss();
		}

	};
	
	
	private void handlUserInfo() {
		UserDao dao = new UserDao(this);
		if(isChecked){
			UserInfoJsonBean bean = new UserInfoJsonBean();
			bean.setUserName(userName);
			bean.setPassword(password);
			dao.deleteAllUser();
			dao.insertUserInfo(bean);
//			UserInfoJsonBean bean1 = dao.getUserInfo();
//			Log.i("HHH",bean1.getUserName());
			IMPApplication.getInstance().sp.edit().putBoolean("remberMe", true).commit();
		} else {
			IMPApplication.getInstance().sp.edit().putBoolean("remberMe", false).commit();
			dao.deleteAllUser();
		}
		
		IMPApplication.getInstance().setOpenId(openId);
		
		Intent i = new Intent(this,MainActivity.class);
		startActivity(i);
		
		this.finish();
		
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView(R.layout.login);

		Intent i = getIntent();
		userName = i.getStringExtra("userName");
		password = i.getStringExtra("password");
		
		findViews();
		initRes();
		setValues();
		setListeners();
		
	}

	private void findViews() {
		btnLogin = (Button) findViewById(R.id.btn_login);
		ivCheckbox = (ImageView) findViewById(R.id.iv_checkbox);
		etUserName = (EditText) findViewById(R.id.et_user_name);
		etPassword = (EditText) findViewById(R.id.et_pas);
		
		pd = CommonsUtil.createLoadingDialog(this, "登录中,请稍后~");

	}

	private void initRes() {
		UserDao dao = new UserDao(this);
		UserInfoJsonBean bean = dao.getUserInfo();
		if(bean != null){
			userName = bean.getUserName();
			password = bean.getPassword();
			Log.i("TEST",userName);
			Log.i("TEST",password);
		}
	}

	private void setValues() {
		// TODO Auto-generated method stub
		etUserName.setText(userName);
		etPassword.setText(password);
	}

	private void setListeners() {
		ivCheckbox.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (isChecked) {
					ivCheckbox.setBackgroundDrawable(getResources()
							.getDrawable(R.drawable.sift_radio_unselected));
					isChecked = false;
				} else {
					ivCheckbox.setBackgroundDrawable(getResources()
							.getDrawable(R.drawable.sift_radio_selected));
					isChecked = true;
				}
			}
		});
		
		btnLogin.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(valid()){
					login();
					pd.show();
				}
			}
		});
		

	}
	
	private void login(){
		new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					openId = IMPApplication.getInstance().userService.login(userName,password);
					if(!"".equals(openId)){
						handler.sendEmptyMessage(LOGIN_SECUSS);
					} else {
						handler.sendEmptyMessage(LOGIN_FAILED);
					}
				} catch(Exception e) {
					handler.sendEmptyMessage(Defines.HTTP_REQUEST_TIMEOUT);
					e.printStackTrace();
				}
				
			}
		}).start();
	}
	
	
	/**
	 * 验证用户名和密码是否有效
	 * @return
	 */
	private boolean valid(){
		userName = etUserName.getText().toString();
		password = etPassword.getText().toString();
		if("".equals(userName)){
			Toast.makeText(this, "请输入帐号！", Toast.LENGTH_SHORT).show();
			return false;
		}
		if("".equals(password)){
			Toast.makeText(this, "请输入密码！", Toast.LENGTH_SHORT).show();
			return false;
		}
		return true;
	}
	

}
